﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;

using Hockey.Shared;
using Hockey.UILayer.Controls;
using Hockey.UILayer.Views;
using Microsoft.Practices.Unity;
using Hockey.UILayer.ViewModels;
using Microsoft.Practices.Prism.Events;

namespace Hockey.UILayer
{
    /// <summary>
    /// UI Layer Module Class
    /// </summary>
    public class UILayerModule: IModule
    {
        private IRegionManager _regions;
        private IUnityContainer _unity;
        private IEventAggregator _events;

        /// <summary>
        /// Constructor
        /// </summary>
        public UILayerModule(IRegionManager rg, IUnityContainer unity, IEventAggregator events)
        {
            if (rg == null)
                throw new ArgumentNullException("RegionManager");
            _regions = rg;

            if (unity == null)
                throw new ArgumentNullException("Unity");
            _unity = unity;

            if (events == null)
                throw new ArgumentNullException("Events");
            _events = events;
        }

        /// <summary>
        /// Initialization
        /// </summary>
        public void Initialize()
        {
            // register views
            _regions.RegisterViewWithRegion(RegionNames.MiddleRegion, typeof(BoardControl));
            _regions.RegisterViewWithRegion(RegionNames.CompassRegion, typeof(CompassControl));
            _regions.RegisterViewWithRegion(RegionNames.MenuRegion, typeof(HockeyMenu));
            _regions.RegisterViewWithRegion(RegionNames.ProgressRegion, typeof(GameInfoView));
            _regions.RegisterViewWithRegion(RegionNames.NavigationBarRegion, typeof(NavigationBar));
            _regions.RegisterViewWithRegion(RegionNames.TopPlayerLabel, () => { return CreatePlayerLabel(ePuckDir.Down, "Hráč 2"); });
            _regions.RegisterViewWithRegion(RegionNames.BottomPlayerLabel, () => { return CreatePlayerLabel(ePuckDir.Up, "Hráč 1"); });
        }

        /// <summary>
        /// Factory method for player label
        /// </summary>
        private PlayerLabel CreatePlayerLabel(ePuckDir direction, string playerName)
        {
            PlayerLabel pLabel = _unity.Resolve<PlayerLabel>();
            pLabel.PuckDirection = direction;
            (pLabel.DataContext as PlayerLabelVM).PlayerName = playerName;
            (pLabel.DataContext as PlayerLabelVM).PuckDirection = direction;
            return pLabel;
        }
    }
}
